<?php

namespace App\Http\Controllers\Api\V1\Frontend\Table;

use Illuminate\Http\Request;
use App\Services\TableService;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
use Illuminate\Contracts\Filesystem\FileNotFoundException;

class IndexController extends Controller
{
    /**
     * 报表列表数据展示
     * @param Request $request
     * @param TableService $tableService
     * @return mixed
     * @throws ValidationException|FileNotFoundException
     */
    public function index(Request $request,TableService $tableService)
    {
        Validator::make($request->all(),[
            'type' => 'nullable|int|in:1,2,3,4,5,6,7,8,9,10',
        ])->validate();

        //当不存在type的时候，默认type=1
        if (!$request->filled('type')) {
            $request->offsetSet('type',1);
        }

        //当不存在page的时候，默认page=1
        if (!$request->filled('page')) {
            $request->offsetSet('page',1);
        }

        $data = $tableService->index($request->all());

        if ($data['code'] == 200){
            return $this->successful($data['data'],'success');
        }

        return $this->failed($data['message']);
    }

    /**
     * 获取到用户展示的table数据
     * @param TableService $tableService
     * @return mixed
     */
    public function list(TableService $tableService)
    {
        $data = $tableService->tables();

        if ($data['code'] == 200){
            return $this->successful($data['data'],'success');
        }

        return $this->failed($data['message']);
    }

    /**
     * 获取到分类数据展示
     * @param TableService $tableService
     * @return mixed
     */
    public function category(TableService $tableService)
    {
        $data = $tableService->category();

        if ($data['code'] == 200){
            return $this->successful($data['data'],'success');
        }

        return $this->failed($data['message']);
    }

    /**
     * 获取到查询的当前的分类状态
     * @param TableService $tableService
     * @return mixed
     */
    public function currentStatus(TableService $tableService)
    {
        $data = $tableService->currentStatus();

        if ($data['code'] == 200){
            return $this->successful($data['data'],'success');
        }

        return $this->failed($data['message']);
    }
}
